﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TCCHWLib;

namespace TestHardwareMemoryPart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Configuration.LoadConfiguration();
        }
        List<object> m_Cache1 = new List<object>();
        List<object> m_Cache2 = new List<object>();
        private void Allocate_btn_Click(object sender, EventArgs e)
        {
            int address;
            if (Size_Checkbox.Checked)
            {
                if (Processer1.Checked)
                {
                    address = Chip.Instance.Processors[0].Cache.Allocate((int)Size_Num.Value);
                    for (int i = 0; i < Size_Num.Value; i++)
                    {
                        m_Cache1.Add(address + i);
                    }
                }
                else
                {
                    address = Chip.Instance.Processors[0].Cache.Allocate((int)Size_Num.Value);
                    for (int i = 0; i < Size_Num.Value; i++)
                    {
                        m_Cache2.Add(address + i);
                    }
                }
            }
            else
            {
                if (Processer1.Checked)
                    m_Cache1.Add(Chip.Instance.Processors[0].Cache.Allocate());
                else
                    m_Cache2.Add(Chip.Instance.Processors[1].Cache.Allocate());
 
            }
            filldata();

        }

        void filldata()
        {
            SharedMemory.Clear();
            SharedMemory.Text = Chip.Instance.SharedMemory.ToString();
            Cache1.Clear();
            Cache1.Text = Chip.Instance.Processors[0].Cache.ToString();
            Cache2.Clear();
            Cache2.Text = Chip.Instance.Processors[1].Cache.ToString();
            Deallocate_cmbox.Items.Clear();

            address_Write.Items.Clear();
            if (Processer1.Checked)
            {
                Deallocate_cmbox.Items.AddRange(m_Cache1.ToArray());

                address_Write.Items.AddRange(m_Cache1.ToArray());
            }
            else
            {
                Deallocate_cmbox.Items.AddRange(m_Cache2.ToArray());

                address_Write.Items.AddRange(m_Cache2.ToArray());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
              
        }

        private void Size_Checkbox_CheckedChanged(object sender, EventArgs e)
        {
            Size_Num.Enabled = Size_Checkbox.Checked;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            Processer2.Checked = !Processer1.Checked;
            filldata();
        }

        private void Processer2_CheckedChanged(object sender, EventArgs e)
        {
            Processer1.Checked = !Processer2.Checked;
            filldata();
        }

        private void DeAllocate_Click(object sender, EventArgs e)
        {
            if (Processer1.Checked)
            {
                Chip.Instance.Processors[0].Cache.Deallocate(int.Parse(Deallocate_cmbox.Text));
                m_Cache1.Remove(Deallocate_cmbox.Text);
            }
            else
            {
                Chip.Instance.Processors[1].Cache.Deallocate(int.Parse(Deallocate_cmbox.Text));
                m_Cache2.Remove(Deallocate_cmbox.Text);
            }
            filldata();
        }

        private void Write_btn_Click(object sender, EventArgs e)
        {
            if (Processer1.Checked)
                Chip.Instance.Processors[0].Cache.Write(Write_value.Text, int.Parse(address_Write.Text));
            else
                Chip.Instance.Processors[1].Cache.Write(Write_value.Text, int.Parse(address_Write.Text));

            filldata();
        }

        private void read_btn_Click(object sender, EventArgs e)
        {
            if (Processer1.Checked)
                Chip.Instance.Processors[0].Cache.Read(int.Parse(Read_value.Text));
            else
                Chip.Instance.Processors[1].Cache.Read(int.Parse(Read_value.Text));

            filldata();
        }

    }
}
